package com.hq.zb.controller;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hq.zb.dto.CodeDto;
import com.hq.zb.model.Code;
import com.hq.zb.model.CodeLine;
import com.hq.zb.service.ICodeLineService;
import com.hq.zb.service.ICodeService;
import com.hq.zb.utils.Result;
import com.hq.zb.vo.CodeLineVo;
import com.hq.zb.vo.CodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典Controller.
 *
 * @author huanghanrui
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/sys")
public class CodeController {

    @Autowired
    private ICodeService codeService;

    @Autowired
    private ICodeLineService codeLineService;

    /**
     * 保存数据字典
     * @param codeDto
     * @return
     */
    @PostMapping("/code/save")
    public Result saveCode(@RequestBody CodeDto codeDto) {
        codeService.saveCode(codeDto);
        return Result.ok();
    }

    /**
     * 查询数据字典
     * @param codeDto 数据字典
     * @param pageNum 页
     * @param pageSize 页大小
     * @return
     */
    @PostMapping("/code/query")
    @ResponseBody
    public Result queryCode(@RequestBody CodeDto codeDto, int pageNum, int pageSize) {
        Wrapper<CodeVo> wrapper = new EntityWrapper<>();
        wrapper.eq("delete_flag", false);
        wrapper.like("code", codeDto.getCode());
        wrapper.like("name", codeDto.getName());
        Page<CodeVo> page = new Page<>(pageNum, pageSize);
        Page<CodeVo> list = codeService.queryCode(page, wrapper);
        return Result.data(list);
    }

    /**
     * 删除数据字典
     * @param codes 数据字典集合
     * @return
     */
    @PostMapping("/code/delete")
    @ResponseBody
    public Result deleteCodes(@RequestBody List<Code> codes) {
        codeService.deleteCodes(codes);
        return Result.ok();
    }

    /**
     * 获取流水号行表（分页）
     * @param codeDto 数据字典航标
     * @param pageNum 页
     * @param pageSize 页大小
     * @return
     */
    @PostMapping("/code/line/query")
    @ResponseBody
    public Result queryCodeLines(@RequestBody CodeDto codeDto, int pageNum, int pageSize) {
        Wrapper<CodeLineVo> wrapper = new EntityWrapper<>();
        wrapper.eq("delete_flag", false);
        wrapper.eq("code", codeDto.getCode());
        Page<CodeLineVo> page = new Page<>(pageNum, pageSize);
        Page<CodeLineVo> list = codeLineService.queryCode(page, wrapper);
        Result.ok();
        return Result.data(list);
    }

    /**
     * 获取流水号行表（不分页）
     * @param code
     * @return
     */
    @GetMapping("/code/line/{code}")
    @ResponseBody
    public Result queryByCode(@PathVariable("code") String code) {
        List<CodeLineVo> codeLineVos = codeLineService.queryByCode(code);
        String data = JSONObject.toJSONString(codeLineVos, SerializerFeature.NotWriteDefaultValue);
        return Result.data(JSONObject.parse(data));
    }

    /**
     * 删除数据字典行
     * @param codeLines 数据字典集合
     * @return
     */
    @PostMapping("/code/line/delete")
    @ResponseBody
    public Result deleteCodeLines(@RequestBody List<CodeLine> codeLines) {
        codeService.deleteCodeLines(codeLines);
        return Result.ok();
    }
}

